package cn.fl.esign.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.esign.service.IFilSignService;
import cn.fl.esign.vo.FilSignVO;
import cn.fl.esign.voTransform.FilSignVOTransform;
import cn.fl.framework.base.context.CurrentThreadContext;
import tc.esign.facade.esign.api.IContractFlowQueryApiService;
import tc.esign.facade.esign.api.IFilSignApiService;
import tc.esign.facade.esign.condition.FilSignInfoQueryCondition;
import tc.esign.facade.esign.condition.FilSignQueryCondition;
import tc.esign.facade.esign.condition.TcContractFlowQC;
import tc.esign.facade.esign.dto.FilSignDTO;
import tc.esign.facade.esign.dto.TcContractFlowDTO;

/**
 * @Description: 合同签署表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class FilSignServiceImpl implements IFilSignService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IFilSignService.class);
	/**
	 * TODO 自定义 API 暴露服务
	 */
	//@Autowired
	//private I自定义ApiService 自定义ApiService;
	@Autowired
	private IFilSignApiService filSignApiService;

	@Autowired
	private IContractFlowQueryApiService contractFlowQueryApiService;

	@Override
	public FilSignVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		FilSignDTO dto = null;//this.自定义ApiService.selectById(id);
		return FilSignVOTransform.toVO(dto);
	}

	@Override
	public Long add(FilSignVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.filSignApiService.add(FilSignVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.filSignApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(FilSignVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.filSignApiService.modify(FilSignVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return false;
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		logger.info("contractFlowQueryApiService.start");
		Map<String,String> flowTag = new HashMap<>();
		flowTag.put("XD_WKKKD0001","JK_4346,JK_5981,JK_5720,JK_6380,JK_5971,JK_6381,JK_6520,QT_8501,QT_8512,QT_8513,QT_8500,JK_8886,JK_4838,JK_8877,JK_4882,JK_8876,JK_5065,QT_5372,QT_5373,QT_5374,QT_5376,QT_7973");
		flowTag.put("XD_YXDIKD0001","JK_3160,JK_2251,JK_4485,JK_5971,JK_6520,JK_5065,JK_5720");
		if (null == page) {
			page = new Pager();
		}
		try {
			page = this.filSignApiService.searchListPage(page);
			List<FilSignDTO> dtoList = (List<FilSignDTO>) page.getResultList();
			if (CollectionUtils.isNotEmpty(dtoList)) {
				Set<String> businessIdList=new HashSet<>();
				for (FilSignDTO dto : dtoList) {
					if (dto.getFilId()!=null) {
						businessIdList.add(dto.getFilId().toString());
					}
				}
				logger.info("contractFlowQueryApiService.businessIdList:{}",JSON.toJSONString(businessIdList));
				if(CollectionUtils.isNotEmpty(businessIdList)){
					TcContractFlowQC qc=new TcContractFlowQC();
					List<String> ls=new ArrayList(businessIdList);
					qc.setBussinessIdList(ls);
					List<TcContractFlowDTO> contractFlowDTOList=contractFlowQueryApiService.selectContractFlowByList(null,qc);
					logger.info("contractFlowQueryApiService.selectContractFlowByList:{}",JSON.toJSONString(contractFlowDTOList));
					if(CollectionUtils.isNotEmpty(contractFlowDTOList)){
						for (FilSignDTO dto : dtoList) {
							if (StringUtils.isNotEmpty(dto.getTemplateId()) && StringUtils.isNotEmpty(dto.getFilId())) {
								String templateId = dto.getTemplateId();
								Long filId = dto.getFilId();
								List<String> collect = contractFlowDTOList.stream().distinct().filter(m -> StringUtils.isNotEmpty(m.getTemplateCode())
										&& StringUtils.isNotEmpty(m.getBusinessId()) && flowTag.get(m.getTemplateCode().trim()).indexOf(templateId) != -1
										&& filId.longValue() == m.getBusinessId()).map(p -> p.getRelPath()).collect(Collectors.toList());
								if (CollectionUtils.isNotEmpty(collect)) {
									dto.setContractFlowPath(collect.get(0));
									break;
								}
							}
//							for (TcContractFlowDTO tcContractFlowDTO:contractFlowDTOList) {
//
//								String templateCode1 = flowTag.get(tcContractFlowDTO.getTemplateCode().trim());
//								logger.info("contractFlowQueryApiService.selectContractFlowByList-templateCode1{}",templateCode1);
//								if(StringUtils.isBlank(templateCode1)){
//									continue;
//								}
//								boolean b1 = (templateCode1.indexOf(dto.getTemplateId()) != -1);
//								Long businessId = dto.getFilId();
//								Long businessId1 = tcContractFlowDTO.getBusinessId();
//								//云南信托
//								if(businessId==null||businessId1==null){
//									continue;
//								}
//								boolean b = (businessId.longValue() == businessId1.longValue());
//								logger.info("contractFlowQueryApiService.selectContractFlowByList-b1b2{},{}",b1,b);
//
//								if(b1&&b){
//									logger.info("contractFlowQueryApiService.add-{}",tcContractFlowDTO.getRelPath());
//
//									dto.setContractFlowPath(tcContractFlowDTO.getRelPath());
//									break;
//								}
//
//							}
						}
					}
				}
				page.setResultList(dtoList);
			}
		} catch (Exception e) {
			BaseLogUtils.error(logger, "searchListPage", "获取合同服务数据失败!", CurrentThreadContext.getCurrentUserName(), e);
		}
		return page;
	}
	@Override
	public FilSignDTO createFils(List<FilSignDTO> list)throws BaseException{
		if(null == list || list.size() < 1){
			BaseLogUtils.info(logger, "add", "创建合同对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		FilSignDTO dto = this.filSignApiService.createFil(list);
		return dto;
	}
	@Override
	public List<FilSignDTO> getListByMobile(String mobile)throws BaseException{
		return this.filSignApiService.getListByMobile(mobile);
		
	}
	@Override
	public List<FilSignDTO> getListByFilId(FilSignQueryCondition qc)throws BaseException{
		return this.filSignApiService.getListByFilId(qc);
	}
	@Override
	public List<FilSignDTO> getListFilSign(FilSignQueryCondition qc)throws BaseException{
		return this.filSignApiService.getListFilSign(qc);
	}
	@Override
	public Map<String,String> doSignList(FilSignInfoQueryCondition qc) throws BaseException{
		return this.filSignApiService.doSignList(qc);
	}
	@Override
	public FilSignDTO downLoad(FilSignDTO dto) throws BaseException{
		return this.filSignApiService.downLoad(dto);
	}
	@Override
	public List<FilSignDTO> getListFil(FilSignQueryCondition qc)throws BaseException{
		return this.filSignApiService.getListFil(qc);
	}
	@Override
	public Map<String,String> delSign(FilSignQueryCondition qc) throws BaseException{
		return this.filSignApiService.delSign(qc);
	}
	@Override
	public FilSignDTO getUpPdf(FilSignQueryCondition qc)throws BaseException{
		return this.filSignApiService.getUpPdf(qc);
	}
}
